package com.study.ioc.demo.aware;

import com.study.ioc.demo.listener.MyEvent;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * BeanFactoryAware BeanClassLoaderAware BeanFactoryAware ApplicationContextAware
 * 以供有需要的Bean注入你需要的资源，
 * 例如，你只需要实现BeanFactoryAware这个接口，实现setBeanFactory方法，
 * 即可拿到beanFactory实例，Aware接口方便的提供了你所需要的资源
 *
 * @author wuxj
 */
@Component
public class MyAware implements BeanFactoryAware, ApplicationContextAware {

    private BeanFactory beanFactory;

    private ApplicationContext applicationContext;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        MyEvent event = new MyEvent("hello");
        applicationContext.publishEvent(event);
        this.applicationContext = applicationContext;
    }
}
